#!/usr/bin/perl -w
use strict;


sub generate_extended_report_TMT{
		my %data=%{$_[0]};
		my $extended_report_file=$_[1];
		my $limit_ascore=$_[2];
		open GLOBAL,">$extended_report_file";
		print GLOBAL "File\tFirst Scan\tLast scan\tMS\t#Engines\tB\tO\tP\tRejected peptide\tBW peptide w/o mods\tBW peptide with mods\tBW z\tBW Actual Mass\tBW DeltaMass\tBW XCorr\tBW DeltaCn\tBW Sp\tBW RSp\tBW Ions\tBW D\tBW DB first match\tBW Count\t";
		print GLOBAL "_\t";
		print GLOBAL "OMSSA peptide w/o mods\tOMSSA peptide with mods\tz\tOMSSA Calc Neutral Pep Mass\tOMSSA Mass diff\tOMSSA Pvalue\tOMSSA Evalue\tOMSSA DB first match\t";
		print GLOBAL "_\t";
		print GLOBAL "Phenyx peptide w/o mods\tPhenyx peptide with mods\tPhenyx z\tPhenyx mz\tPhenyx Deltamz\tPhenyx z-score\tPhenyx Validity\tPhenyx DB first match\tPhenyx Count\t";
		print GLOBAL "_\t";
		print GLOBAL "Consensus peptide w/o mods\tConsensus peptide with mods\tConsensus z\tConsensus DeltaMz\tConsensus proteins\tPhosphorylated\t_\tTMT_126\tTMT_127\tTMT_128\tTMT_129\tTMT_130\tTMT_131\t_\tAscore revised peptide\tAscore1\tAscore2\tAscore3\t_";
		print GLOBAL "\n";
		
		########	Estadisticas	########
		my $espectros_totales=0;
		my $espectros_fosfo_totales=0;
		my $peptido_correcto_sequest=0;
		my $peptido_correcto_omssa=0;
		my $peptido_correcto_phenyx=0;
		my $peptido_correcto_BO=0;
		my $peptido_correcto_BP=0;
		my $peptido_correcto_OP=0;
		my $peptido_correcto_BOP=0;
		
		my $peptido_correcto_fosfo_sequest=0;
		my $peptido_correcto_fosfo_omssa=0;
		my $peptido_correcto_fosfo_phenyx=0;
		my $peptido_correcto_fosfo_BO=0;
		my $peptido_correcto_fosfo_BP=0;
		my $peptido_correcto_fosfo_OP=0;
		my $peptido_correcto_fosfo_BOP=0;
		
		my %peptidos_no_redundantes;
		my %peptidos_consenso_no_reduntantes;
		my %peptidos_fosforilados_no_redundantes;
		my %peptidos_fosforilados_no_redundantes_ascore;
		
		####################################			
		foreach my $spectrum(keys %data){
			#Solo reporto los consenso en el report Global!!!!
			unless(defined $data{$spectrum}{'consensus_mod'}){
				next;
			}
			my $phospho=0;
			my $numero_fosforilaciones;
			my $peptido_conteo_fosfos=$data{$spectrum}{'consensus_mod'};
			while($peptido_conteo_fosfos=~s/(\(21\)|\(21\,|21\))/x/){
				$numero_fosforilaciones++;
			}
			while($peptido_conteo_fosfos=~s/(\(23\)|\(23\,|23\))/x/){
				$numero_fosforilaciones++;#en realidad esto son DH,...
			}
			if($numero_fosforilaciones){
				$phospho=1;
				my($a1,$a2,$a3)=(0,0,0);
				if(defined $data{$spectrum}{'ascore_data'}){
					if(defined $data{$spectrum}{'ascore_data'}{'ascore'}[0]){
						if($data{$spectrum}{'ascore_data'}{'ascore'}[0]>$limit_ascore){
							$a1=1;
						}
					}
					else{
						print $spectrum,"\n";
						$a1=6666;
					}
					if(defined $data{$spectrum}{'ascore_data'}{'ascore'}[1]){
						if($data{$spectrum}{'ascore_data'}{'ascore'}[1]>$limit_ascore){
							$a2=1;
						}
					}
					if(defined $data{$spectrum}{'ascore_data'}{'ascore'}[2]){
						if($data{$spectrum}{'ascore_data'}{'ascore'}[2]>$limit_ascore){
							$a3=1;
						}
					}
					
					if(defined $peptidos_fosforilados_no_redundantes_ascore{$data{$spectrum}{'consensus_mod'}}){
						$peptidos_fosforilados_no_redundantes_ascore{$data{$spectrum}{'consensus_mod'}}[0]+=$a1;
						$peptidos_fosforilados_no_redundantes_ascore{$data{$spectrum}{'consensus_mod'}}[1]+=$a2;
						$peptidos_fosforilados_no_redundantes_ascore{$data{$spectrum}{'consensus_mod'}}[2]+=$a3;
					}
					else{
						$peptidos_fosforilados_no_redundantes_ascore{$data{$spectrum}{'consensus_mod'}}[0]=$a1;
						$peptidos_fosforilados_no_redundantes_ascore{$data{$spectrum}{'consensus_mod'}}[1]=$a2;
						$peptidos_fosforilados_no_redundantes_ascore{$data{$spectrum}{'consensus_mod'}}[2]=$a3;
					}
				
				}
			}
			my $print_spec=$spectrum;
			$print_spec=~s/\./\t/g;
			print GLOBAL $print_spec,"\t";
			print GLOBAL $data{$spectrum}{'ms'},"\t";
			my $engines=0;
			if((defined $data{$spectrum}{'sequest'}{'unmodified_sequence'})and(defined $data{$spectrum}{'omssa'}{'unmodified_sequence'})and(defined $data{$spectrum}{'phenyx'}{'unmodified_sequence'})){
				if(($data{$spectrum}{'sequest'}{'unmodified_sequence'} eq $data{$spectrum}{'omssa'}{'unmodified_sequence'})and($data{$spectrum}{'sequest'}{'unmodified_sequence'} eq $data{$spectrum}{'phenyx'}{'unmodified_sequence'})){
					$engines=3;
				}
				else{
					$engines=2;
				}
			}
			elsif((defined $data{$spectrum}{'sequest'})and(defined $data{$spectrum}{'omssa'})){
				$engines=2;
			}
			elsif((defined $data{$spectrum}{'sequest'})and (defined $data{$spectrum}{'phenyx'})){
				$engines=2;
			}
			elsif((defined $data{$spectrum}{'omssa'})and(defined $data{$spectrum}{'phenyx'})){
				$engines=2;
			}
			print GLOBAL $engines,"\t";
			
			my ($b,$o,$p,$rejected_peptide);
			$b=0;
			$o=0;
			$p=0;
			$rejected_peptide='';
			
			if((defined $data{$spectrum}{'sequest'}{'unmodified_sequence'})and($data{$spectrum}{'sequest'}{'unmodified_sequence'} eq $data{$spectrum}{'consensus'})){
				$b=1;
			}
			else{
				if(defined $data{$spectrum}{'sequest'}{'unmodified_sequence'}){
					$rejected_peptide=$data{$spectrum}{'sequest'}{'modified_sequence'};
				}
				
			}
			if((defined $data{$spectrum}{'omssa'}{'unmodified_sequence'})and($data{$spectrum}{'omssa'}{'unmodified_sequence'} eq $data{$spectrum}{'consensus'})){
				$o=1;
			}
			else{
				if(defined $data{$spectrum}{'omssa'}{'unmodified_sequence'}){
					$rejected_peptide=$data{$spectrum}{'omssa'}{'modified_sequence'};
				}
			}
				
			if((defined $data{$spectrum}{'phenyx'}{'unmodified_sequence'})and($data{$spectrum}{'phenyx'}{'unmodified_sequence'} eq $data{$spectrum}{'consensus'})){
				$p=1;
			}
			else{
				if(defined $data{$spectrum}{'phenyx'}{'unmodified_sequence'}){
					$rejected_peptide=$data{$spectrum}{'phenyx'}{'modified_sequence'};
				}
			}
			
			#	conteo estadisticas
			$espectros_totales++;
			if($phospho){
				$espectros_fosfo_totales++;
			}
			if(defined $peptidos_consenso_no_reduntantes{$data{$spectrum}{'consensus_mod'}}){
				$peptidos_consenso_no_reduntantes{$data{$spectrum}{'consensus_mod'}}++;
			}
			else{
				$peptidos_consenso_no_reduntantes{$data{$spectrum}{'consensus_mod'}}=1;
			}
			if($b and $o and $p){
				$peptido_correcto_sequest++;
				$peptido_correcto_omssa++;
				$peptido_correcto_phenyx++;
				$peptido_correcto_BO++;
				$peptido_correcto_BP++;
				$peptido_correcto_OP++;
				$peptido_correcto_BOP++;
				if(defined $peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}){
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[0]++;
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[1]++;
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[2]++;
				}
				else{
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[0]=1;
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[1]=1;
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[2]=1;
				}
				if($phospho){
					$peptido_correcto_fosfo_sequest++;
					$peptido_correcto_fosfo_omssa++;
					$peptido_correcto_fosfo_phenyx++;
					$peptido_correcto_fosfo_BO++;
					$peptido_correcto_fosfo_BP++;
					$peptido_correcto_fosfo_OP++;
					$peptido_correcto_fosfo_BOP++;
					if(defined $peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}){
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[0]++;
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[1]++;
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[2]++;
					}
					else{
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[0]=1;
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[1]=1;
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[2]=1;
					}
				}
			}
			elsif($b and $o){
				$peptido_correcto_sequest++;
				$peptido_correcto_omssa++;
				$peptido_correcto_BO++;
				if(defined $peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}){
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[0]++;
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[1]++;
				}
				else{
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[0]=1;
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[1]=1;
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[2]=0;
				}
				if($phospho){
					$peptido_correcto_fosfo_sequest++;
					$peptido_correcto_fosfo_omssa++;
					$peptido_correcto_fosfo_BO++;
					if(defined $peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}){
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[0]++;
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[1]++;
					}
					else{
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[0]=1;
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[1]=1;
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[2]=0;
					}
				}
			}
			elsif($b and $p){
				$peptido_correcto_sequest++;
				$peptido_correcto_phenyx++;
				$peptido_correcto_BP++;
				if(defined $peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}){
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[0]++;
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[2]++;
				}
				else{
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[0]=1;
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[1]=0;
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[2]=1;
				}
				if($phospho){
					$peptido_correcto_fosfo_sequest++;
					$peptido_correcto_fosfo_phenyx++;
					$peptido_correcto_fosfo_BP++;
					if(defined $peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}){
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[0]++;
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[2]++;
					}
					else{
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[0]=1;
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[1]=0;
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[2]=1;
					}
				}
			}
			elsif($o and $p){
				$peptido_correcto_omssa++;
				$peptido_correcto_phenyx++;
				$peptido_correcto_OP++;
				if(defined $peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}){
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[1]++;
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[2]++;
				}
				else{
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[0]=0;
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[1]=1;
					$peptidos_no_redundantes{$data{$spectrum}{'consensus_mod'}}[2]=1;
				}
				if($phospho){
					$peptido_correcto_fosfo_omssa++;
					$peptido_correcto_fosfo_phenyx++;
					$peptido_correcto_fosfo_OP++;
					if(defined $peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}){
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[1]++;
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[2]++;
					}
					else{
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[0]=0;
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[1]=1;
						$peptidos_fosforilados_no_redundantes{$data{$spectrum}{'consensus_mod'}}[2]=1;
					}
				}
			}
			
			#	fin del conteo estadisticas
			
			
			print GLOBAL $b,"\t",$o,"\t",$p,"\t",$rejected_peptide,"\t";
			if((defined $data{$spectrum}{'sequest'}{'unmodified_sequence'})and($data{$spectrum}{'sequest'}{'unmodified_sequence'} eq $data{$spectrum}{'consensus'})){
				print GLOBAL $data{$spectrum}{'sequest'}{'unmodified_sequence'},"\t";
				print GLOBAL $data{$spectrum}{'sequest'}{'modified_sequence'},"\t";
				print GLOBAL $data{$spectrum}{'sequest'}{'z'},"\t";
				print GLOBAL $data{$spectrum}{'sequest'}{'actual_mass'},"\t";
				print GLOBAL $data{$spectrum}{'sequest'}{'deltamass'},"\t";
				print GLOBAL $data{$spectrum}{'sequest'}{'xcorr'},"\t";
				print GLOBAL $data{$spectrum}{'sequest'}{'deltacn'},"\t";
				print GLOBAL $data{$spectrum}{'sequest'}{'sp'},"\t";
				print GLOBAL $data{$spectrum}{'sequest'}{'rsp'},"\t";
				print GLOBAL $data{$spectrum}{'sequest'}{'ions'},"\t";
				print GLOBAL $data{$spectrum}{'sequest'}{'d'},"\t";
				if(defined $data{$spectrum}{'sequest'}{'db_first_match'}){
					print GLOBAL $data{$spectrum}{'sequest'}{'db_first_match'},"\t";
				}
				else{
					print GLOBAL 'NA',"\t";
				}
				
				print GLOBAL $data{$spectrum}{'sequest'}{'count'},"\t";
			}
			else{
				for(my $i=0;$i<13;$i++){
					print GLOBAL "\t";
				}
			}
			print GLOBAL "+\t";
			
			if((defined $data{$spectrum}{'omssa'}{'unmodified_sequence'})and($data{$spectrum}{'omssa'}{'unmodified_sequence'} eq $data{$spectrum}{'consensus'})){
				print GLOBAL $data{$spectrum}{'omssa'}{'unmodified_sequence'},"\t";
				print GLOBAL $data{$spectrum}{'omssa'}{'modified_sequence'},"\t";
				print GLOBAL $data{$spectrum}{'omssa'}{'z'},"\t";
				print GLOBAL $data{$spectrum}{'omssa'}{'calc_neutral_pep_mass'},"\t";
				print GLOBAL $data{$spectrum}{'omssa'}{'massdiff'},"\t";
				print GLOBAL $data{$spectrum}{'omssa'}{'pvalue'},"\t";
				print GLOBAL $data{$spectrum}{'omssa'}{'expect'},"\t";
				#print GLOBAL $data{$spectrum}{'omssa'}{'ions'},"\t";
				#print GLOBAL $data{$spectrum}{'omssa'}{'num_hits'},"\t";
				print GLOBAL $data{$spectrum}{'omssa'}{'db_first_match'},"\t";
				#print GLOBAL $data{$spectrum}{'omssa'}{'count'},"\t";
			}
			else{
				for(my $i=0;$i<8;$i++){
					print GLOBAL "\t";
				}
			}
			print GLOBAL "+\t";
			
			if((defined $data{$spectrum}{'phenyx'}{'unmodified_sequence'})and($data{$spectrum}{'phenyx'}{'unmodified_sequence'} eq $data{$spectrum}{'consensus'})){
				print GLOBAL $data{$spectrum}{'phenyx'}{'unmodified_sequence'},"\t";
				print GLOBAL $data{$spectrum}{'phenyx'}{'modified_sequence'},"\t";
				print GLOBAL $data{$spectrum}{'phenyx'}{'z'},"\t";
				print GLOBAL $data{$spectrum}{'phenyx'}{'mz'},"\t";
				print GLOBAL $data{$spectrum}{'phenyx'}{'deltamz'},"\t";
				print GLOBAL $data{$spectrum}{'phenyx'}{'zscore'},"\t";
				print GLOBAL $data{$spectrum}{'phenyx'}{'validity'},"\t";
				print GLOBAL $data{$spectrum}{'phenyx'}{'db_first_match'},"\t";
				print GLOBAL $data{$spectrum}{'phenyx'}{'count'},"\t";
			}
			else{
				for(my $i=0;$i<9;$i++){
					print GLOBAL "\t";
				}
			}
			print GLOBAL "+\t";
			
			
			print GLOBAL $data{$spectrum}{'consensus'},"\t";
			print GLOBAL $data{$spectrum}{'consensus_mod'},"\t";
			print GLOBAL $data{$spectrum}{'consensus_z'},"\t";
			if(defined $data{$spectrum}{'consensus_deltamz'}){
				print GLOBAL $data{$spectrum}{'consensus_deltamz'},"\t";
			}
			else{
				print GLOBAL "0\t";
			}
			
			print GLOBAL $data{$spectrum}{'consensus_proteins'},"\t";
			print GLOBAL $phospho,"\t+\t";
			if(defined $data{$spectrum}{'tmt_info'}){
				my $tmt_info=$data{$spectrum}{'tmt_info'};
				my ($t126,$t127,$t128,$t129,$t130,$t131);
				if(($t126,$t127,$t128,$t129,$t130,$t131)=split /\|/,$tmt_info){
					print GLOBAL $t126,"\t",$t127,"\t",$t128,"\t",$t129,"\t",$t130,"\t",$t131,"\t";
				}
				else{
					print GLOBAL "0\t","0\t","0\t","0\t","0\t","0\t";
				}
				
			}
			else{
				print GLOBAL "0\t","0\t","0\t","0\t","0\t","0\t";
			}
			print GLOBAL "+\t";
			my $cuenta_ascores=0;
			if(defined $data{$spectrum}{'ascore_data'}{'ascore'}){
				print GLOBAL $data{$spectrum}{'ascore_data'}{'peptide'},"\t";
				my @ascores=@{$data{$spectrum}{'ascore_data'}{'ascore'}};
				foreach(@ascores){
					$cuenta_ascores++;
					print GLOBAL $_,"\t";
				}
				for (my $t=0;$t<(3-$cuenta_ascores);$t++){
					print GLOBAL "\.\t";
				}
			}
			else{
				if(defined $data{$spectrum}{'consensus_mod'}){
					if($data{$spectrum}{'consensus_mod'}=~/(\(21\)|\(23\))/){
						print GLOBAL $data{$spectrum}{'consensus_mod'},"\.\t","\.\t","\.\t","\.\t";
					}
					else{
						print GLOBAL "\.\t","\.\t","\.\t","\.\t";
					}
				}
				else{
					print GLOBAL "\.\t","\.\t","\.\t","\.\t";
				}
			}
			print GLOBAL "+\n";
		}

	}

